﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmStaffInfo : Form
    {
        private string strStaffID;
        private int oper;

        private string temUserPermission;

        public const int OPER_ADDSTAFF = 1;
        public const int OPER_RMSTAFF = 2;
        public const int OPER_UPDSTAFF = 3;

        public frmStaffInfo(string strStaffID, int oper)
        {
            InitializeComponent();
            this.strStaffID = strStaffID;
            this.oper = oper;

            this.cmb_duty.SelectedIndex = 0;
        }

        private void frmStaffInfo_Load(object sender, EventArgs e)
        {
            DataAccess.UserDAO userDAO = new DataAccess.UserDAO();
            Hashtable ht = (Hashtable)userDAO.getUser(this.strStaffID);
            this.txt_staffID.Text = ht["staffID"].ToString();
            this.txt_username.Text = ht["staffName"].ToString();
            this.cmb_duty.Text = ht["duty"].ToString();

            switch (this.oper)
            {
                case OPER_ADDSTAFF:
                    this.btn_oper.Text = "添加";
                    this.txt_staffID.ReadOnly = false;
                    this.txt_username.ReadOnly = false;
                    this.txt_password.ReadOnly = false;
                    this.btn_permission.Enabled = true;
                    this.cmb_duty.Enabled = true;
                    break;
                case OPER_RMSTAFF:
                    this.btn_oper.Text = "移除";
                    this.txt_staffID.ReadOnly = true;
                    this.txt_username.ReadOnly = true;
                    this.txt_password.ReadOnly = true;
                    this.btn_permission.Enabled = false;
                    this.cmb_duty.Enabled = false;
                    break;
                case OPER_UPDSTAFF:
                    this.btn_oper.Text = "修改";
                    this.txt_staffID.ReadOnly = true;
                    this.txt_username.ReadOnly = false;
                    this.txt_password.ReadOnly = false;
                    this.btn_permission.Enabled = true;
                    this.cmb_duty.Enabled = true;
                    break;
                default:
                    this.Close();
                    break;
            }
        }

        private void btn_oper_Click(object sender, EventArgs e)
        {
            DataAccess.UserDAO userDAO = new DataAccess.UserDAO();
            string strShow;
            switch (this.oper)
            {
                case OPER_ADDSTAFF:
                    if (userDAO.isLoginIDExist(this.txt_staffID.Text.Trim()))
                    {
                        strShow = "添加新员工" + this.txt_staffID.Text.Trim() + "失败，原因：该员工ID已存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    userDAO.addNewUser(
                                        LoginUser.getCurrOper(),
                                        this.txt_staffID.Text.Trim(),
                                        this.txt_username.Text.Trim(),
                                        this.txt_password.Text.Trim(),
                                        this.temUserPermission,
                                        this.cmb_duty.SelectedIndex+1);
                    break;
                case OPER_RMSTAFF:
                    if (userDAO.isLoginIDExist(this.txt_staffID.Text.Trim()))
                    {
                        userDAO.removeUser(LoginUser.getCurrOper(), this.txt_staffID.Text.Trim());
                    }
                    else
                    {
                        strShow = "删除员工" + this.txt_staffID.Text.Trim() + "失败，原因：该员工ID不存在！";
                        MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    break;
                case OPER_UPDSTAFF:
                    break;
                default:
                    this.Close();
                    break;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_permission_Click(object sender, EventArgs e)
        {
            frmEditPermission frmD = new frmEditPermission(temUserPermission);
        }

        
    }
}
